"""
alembic数据库迁移工具
"""

import os
import sys

path = os.path.dirname(__file__)
sys.path.append(os.path.join(path, '..'))

from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.orm import relationship, backref
from conf.mysql_config import BASE


class Province(BASE):
    __tablename__ = 'province'
    id = Column(Integer, autoincrement=True, primary_key=True)
    province = Column(String(50), nullable=False, comment='省', doc={'zh': '省'})
    create_time = Column(DateTime, default=datetime.now, comment='创建时间', doc={'zh': '创建时间'})
    update_time = Column(DateTime, default=datetime.now, comment='更新时间', doc={'zh': '更新时间'})

    def __str__(self):
        return f'<Province id={self.id}, province={self.province}>'

    __repr__ = __str__


class City(BASE):
    __tablename__ = 'city'
    id = Column(Integer, autoincrement=True, primary_key=True)
    city = Column(String(50), nullable=False, comment='市', doc={'zh': '市'})
    create_time = Column(DateTime, default=datetime.now, comment='创建时间', doc={'zh': '创建时间'})
    update_time = Column(DateTime, default=datetime.now, comment='更新时间', doc={'zh': '更新时间'})
    province_id = Column(Integer, ForeignKey('province.id'), comment='省ID', doc={'zh': '省ID'})
    province = relationship('Province', backref=backref('cities', uselist=True))

    def __str__(self):
        return f'<City id={self.id}, province={self.city}>'

    __repr__ = __str__


BASE.metadata.create_all()
